package com.controller.login;


import com.comm.R;
import com.entity.Employee;
import com.service.setting.SysEmployeeService;
import com.utils.JwtUtils;
import io.swagger.annotations.*;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@RestController
@RequestMapping(value = "/login")
@Api(value = "用户登录模块",tags = "登录的所有接口")
public class LoginController {

    @Autowired
    private SysEmployeeService sysEmployeeService;

    /**
     * 登录
     * @param empUserName
     * @param empPassword
     * @return
     */
    @GetMapping(value = "/sulogin")
    @ApiOperation(value = "登录",notes = "登录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "empUserName",value = "用户名",paramType = "query",dataType = "String",required = true),
            @ApiImplicitParam(name = "empPassword",value = "密码",paramType = "query",dataType = "String",required = true)
    })
    public R login(String empUserName, String empPassword, HttpServletResponse response){

               R r= sysEmployeeService.login(empUserName,empPassword,response);
                if (r.getCode()==200){
//                    this.userName=empUserName;
                    Subject subject = SecurityUtils.getSubject();
                    Session session = subject.getSession();
                    session.setTimeout(JwtUtils.TTMILLIS);
                }

        return r;
    }

    /**
     * 初始化菜单
     * @return
     */
    @GetMapping(value = "/initMenu")
    @ApiOperation(value = "这个是登录后初始化菜单的接口，必须登录后才能用",notes = "初始化菜单")
    public R initMenu(HttpServletRequest request){
        Cookie[] cookies = request.getCookies();
        for (Cookie c :cookies) {


            System.out.println("Name=============="+c.getName());
            System.out.println("value================="+c.getValue());
        }
        R r=R.defaulted();
        Subject subject = SecurityUtils.getSubject();
        String name=null;
        if (!ObjectUtils.isEmpty(subject)){
            Object principal = subject.getPrincipal();
            if (principal!=null){
                name= principal.toString();
            }

        }
        if (name!=null){
          r = sysEmployeeService.getEmpByUserName(name);
        }

        Employee employee=(Employee)r.getData();
        Integer id=null;
        if (employee!=null){
           id = employee.getId();
        }

       return sysEmployeeService.initMenuByEmpId(id);


    }


    /**
     * 登录后得到当前用户信息
     * @return
     */
    @GetMapping("/getCurrentEmp")
    @ApiOperation(value = "这个是登录后主页的用户信息，必须登录后才能用",notes = "获取当前用户信息")
    public R getEmpBaseMsg(){

        R r=R.defaulted().setMsg("未登录！");
        Subject subject = SecurityUtils.getSubject();
        String name=null;
        if (!ObjectUtils.isEmpty(subject)){
            Object principal = subject.getPrincipal();
            if (principal!=null){
                name= principal.toString();
            }

        }
        if (!ObjectUtils.isEmpty(name)){
                 r = sysEmployeeService.getEmpByUserName(name);
            }
         return r;

    }


    @PostMapping("/logOut")
    @ApiOperation(value = "这个是登录后退出登录功能，必须登录后才能用",notes = "退出登录")
    public R  logOut(){

        SecurityUtils.getSubject().logout();
        return  R.ok().setMsg("登出成功！");
    }

}
